﻿using TaskKernel.Domain;
using TaskKernel.Domain.Services.Permission;
using TaskKernel.Domain.Services.Role;
using Xunit;

namespace TaskKernel.Tests
{
    public class PermissionServiceTest
    {
        [Fact]
        public void CheckOwnerRoleInClaimDepartment()
        {
            var i = new IPermission<TaskPermissions>[]
                        {
                            new AdminClosedEditF(),
                            new ExecutorOwnerClosedEditF(),
                            new AdminUnclosedEditF(),
                            new ExecutorUnclosedEditF(),
                            new OwnerUnclosedClaimEditF(),
                            new OwnerUnclosedNotClaimEditF()
                        };
            var account = new Account("olegk");
            account.SetMail("olegk@servertd.spb.ru");
            Task task = new DomainModelTest().create_task(account, "taskName");
            var department = new Department("efwfe") { IsClaim = true };
            task.SetDepartment(account, department);

            var role = RoleEnum(task, account);
            var permission = new TaskPermissions(role, task);
            foreach (var permission1 in i)
            {
                permission.AddPermissions(permission1);
            }
            var t= permission.GetPermissions();
            Assert.Equal(t.GetType(), new PermissionsOtherCreatePage().GetType());
            department = new Department("efwfe") { IsClaim = false };
            task.SetDepartment(account, department);

            role = RoleEnum(task, account);
            permission = new TaskPermissions(role, task);
            foreach (var permission1 in i)
            {
                permission.AddPermissions(permission1);
            }
            t=permission.GetPermissions();
            Assert.Equal(t.GetType(), new PermissionsOwnerUnclosedTaskEditPage().GetType());
            task.SetStatus(TaskStatus.Closed);
            role = RoleEnum(task, account);
            permission = new TaskPermissions(role, task);
            foreach (var permission1 in i)
            {
                permission.AddPermissions(permission1);
            }
            t=permission.GetPermissions();
            Assert.Equal(t.GetType(), new PermissionsOwnerClosedTaskEditPage().GetType());
        }

        private static RoleEnum RoleEnum(Task task, Account account)
        {
            var roleInDepartment = new RoleInDepartment(task.Department, account);
            var role = roleInDepartment.GetRole();
            var roleInTask = new RoleInTask(task, account, role);
            role = roleInTask.GetRole();
            return role;
        }
    }
}